package org.cloud.im.admin.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;
import org.cloud.im.admin.account.ao.AccountQueryAO;
import org.cloud.im.admin.account.ao.LoginAO;
import org.cloud.im.admin.account.bo.UserInfoBO;
import org.cloud.im.admin.account.listener.UserImportEventListener;
import org.cloud.im.admin.account.mapper.AccountMapper;
import org.cloud.im.admin.account.service.IAccountService;
import org.cloud.im.admin.account.vo.AccountVO;
import org.cloud.im.admin.account.vo.UserGroupInfoVO;
import org.cloud.im.admin.common.Result;
import org.cloud.im.admin.common.page.TableDataInfo;
import org.cloud.im.admin.handler.StoreHandler;
import org.cloud.im.core.packets.Group;
import org.cloud.im.core.packets.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: w.weidong.lin
 * @Description: 账号表接口实现层
 * @Date: create in 2021-10-26 10:35:17
 */
@Slf4j
@Service("im.AccountServiceImpl")
public class AccountServiceImpl implements IAccountService {
    @Resource
    private AccountMapper accountMapper;

    @Override
    public TableDataInfo getList(AccountQueryAO queryAO) {
        return getDataTable(accountMapper.getList(queryAO), 100);
    }

    @Override
    public Result login(LoginAO loginAO) {
        /** 数据库
         AccountVO accountVO = accountMapper.getInfoByAccount(loginAO.getAccount());

         if (accountVO == null) {
         return Result.getServiceError("账号不存在");
         }

         if (accountVO.getPassword() != null && accountVO.getPassword().equals(loginAO.getPassword())) {
         return Result.getSuccessResult(accountVO);
         } else {
         return Result.getServiceError("密码不正确");
         }
         **/
        if (loginAO.getAccount() == null || loginAO.getPassword() == null) {
            return Result.getParamException("100", "账号、密码为空");
        }

        User userBo = StoreHandler.userInfoMap.get(loginAO.getAccount());
        if (userBo == null) {
            return Result.getServiceError("账号不存在");
        }
        /** TODO 咱不考虑密码验证
         if (!loginAO.getPassword().equals(userBo.getPassword())) {
         return Result.getServiceError("密码不正确");
         }
         **/
        return Result.getSuccessResult(userBo);
    }

    @Override
    public Result initOrgUserInfo(String inputFilePath) {
        UserImportEventListener userListener = new UserImportEventListener();
        EasyExcel.read(inputFilePath, UserInfoBO.class, userListener).sheet(0).doRead();
        StoreHandler.deptInfoMap = userListener.users.stream().collect(Collectors.groupingBy(o -> o.getDeptNo()));
        StoreHandler.userGroupMap = userListener.users.stream().collect(Collectors.groupingBy(o -> o.getEmpNo()));
        StoreHandler.setUseInfoMap(userListener.usersMap);
        return Result.getSuccessResult(null);
    }

    @Override
    public List<UserGroupInfoVO> getUserGroup(String empNo) {
        List<UserGroupInfoVO> groupInfos = new ArrayList<>();
        List<Group> groups = StoreHandler.getUserGroup(empNo);
        if (groups != null) {
            groups.forEach(g -> {
                UserGroupInfoVO info = new UserGroupInfoVO();
                BeanUtil.copyProperties(g, info);
                info.setOneliststare(false);
                groupInfos.add(info);
            });
        }
        return groupInfos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(String account, String name) {
        AccountVO accountVO = accountMapper.getInfoByAccount(account);
        String oldName = accountVO.getName();
        if (accountVO == null) {
            return false;
        }

        accountVO.setName(name);

        accountMapper.update(accountVO);

        AccountVO newAcccount = accountMapper.getInfoByAccount(account);
        log.error("原始名称{}，更改后名称{}", oldName, newAcccount.getName());
        return true;
    }

    protected <T> TableDataInfo getDataTable(List<T> list, Integer total) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
    }
}
